Overview
Theadvanced_metrics_processor.py script enriches the base analysis with advanced metrics derived from historical OHLCV (Open, High, Low, Close, Volume) data. It processes CSV files containing price and volume data to calculate technical metrics that require time series analysis.
Purpose
This script adds sophisticated technical analysis metrics including:- Volume analysis (RVOL, turnover, EMA volume)
- Volatility metrics (ADR - Average Daily Range)
- Price benchmarks (ATH, 52-week low, historical returns)
- Gap analysis and intraday range calculations
- Circuit limit mapping
Input Files Required
Base analysis file generated by
bulk_market_analyzer.py. This is both input and output.NSE price band (circuit limit) data containing current circuit limit percentages for each symbol.
Directory containing individual CSV files for each stock with daily OHLCV data. Files named as
{SYMBOL}.csv.OHLCV CSV Format
Each CSV file should contain:Output Produced
Updates the master analysis file in-place by adding/updating advanced metric fields for each stock.
Processing Logic
1. EMA Calculation
Implements exponential moving average calculation:2. Per-Symbol CSV Processing
Each stock’s OHLCV file is processed independently using concurrent execution:3. All-Time High (ATH) Calculation
With hybrid fix for live price integration:4. Gap Analysis
Calculates opening gap relative to previous close:5. Average Daily Range (ADR) Calculation
Calculates volatility across multiple timeframes:6. Multi-Period Returns
Calculates historical returns over various lookback periods:7. Volume Metrics Processing
8. Turnover Moving Averages
9. Parallel Processing
Uses ThreadPoolExecutor for efficient batch processing:10. Circuit Limit Integration
Fields Added/Modified
This script adds/updates the following fields in the master JSON:Volume Metrics
- 30 Days Average Rupee Volume(Cr.): Average daily turnover over 30 days
- RVOL: Relative volume (current volume / 20-day average volume)
- Daily Rupee Turnover 20(Cr.): 20-day moving average of daily turnover
- Daily Rupee Turnover 50(Cr.): 50-day moving average of daily turnover
- Daily Rupee Turnover 100(Cr.): 100-day moving average of daily turnover
- 200 Days EMA Volume: 200-period exponential moving average of volume
- % from 52W High 200 Days EMA Volume: Distance from 52-week high of EMA volume
Volatility Metrics
- 5 Days MA ADR(%): 5-day moving average of average daily range
- 14 Days MA ADR(%): 14-day moving average of average daily range
- 20 Days MA ADR(%): 20-day moving average of average daily range
- 30 Days MA ADR(%): 30-day moving average of average daily range
- Day Range(%): Intraday high-low range as percentage
Price Benchmarks
- % from ATH: Distance from all-time high (with live price correction)
- Gap Up %: Opening gap from previous close (replaces placeholder)
- 6 Month Returns(%): Price return over 6 months (~126 trading days)
- % from 52W Low: Distance from 52-week low
Market Structure
- Circuit Limit: NSE circuit limit percentage (price band)
Code Example
advanced_metrics_processor.py
Function Reference
calculate_ema(series, periods)
Calculates exponential moving average using pandas.
Parameters:
series: Pandas Series of numeric valuesperiods: Number of periods for EMA calculation
process_symbol_csv(csv_path)
Processes a single stock’s OHLCV CSV file and calculates all advanced metrics.
Parameters:
csv_path: Full path to the CSV file
main()
Orchestrates the entire processing pipeline including loading data, parallel processing, and updating the master JSON.
Returns: None (writes output to JSON file)
Performance Notes
- Parallel Processing: Uses ThreadPoolExecutor with 10 workers for concurrent CSV processing
- Processing Time: ~2,000 stocks processed in 10-20 seconds
- Memory Efficiency: Processes one CSV at a time per thread
- Error Handling: Gracefully handles missing/corrupt CSV files
- Hybrid Fix: Eliminates 1-day lag in ATH calculation by using live LTP when available
Dependencies
pandas: DataFrame operations and EMA calculationsjson: JSON file handlingos: File path operationsglob: File pattern matchingconcurrent.futures: Parallel processing
Important Notes
- Dependency: Must run after
bulk_market_analyzer.py - In-Place Update: Modifies the master JSON file directly
- Data Freshness: ATH calculation uses hybrid approach combining historical data with live prices
- Trading Days: Assumes ~252 trading days per year, ~126 for 6 months
- Turnover Calculation: Uses divisor of 10,000,000 to convert to crores
Source File Location
advanced_metrics_processor.py:1-175